package com.neuedu.corejava.ch14.homework;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;

public class SortClient {

	private String host = "localhost";
	private int port = 8888;

	public SortClient() {
		super();
	}

	public SortClient(String host, int port) {
		super();
		this.host = host;
		this.port = port;
	}

	public void sort() {
		Socket socket = null;
		Scanner scanner = null;
		InputStream inputStream = null;
		OutputStream outputStream = null;

		try {
			socket = new Socket(host, port);
			System.out.println("成功连接到服务器！");
			inputStream = socket.getInputStream();
			outputStream = socket.getOutputStream();
			scanner = new Scanner(System.in);
			System.out.print("请输入一串数字，并以空格分隔：");
			String intString = scanner.nextLine();
			outputStream.write(intString.getBytes());
			System.out.println();
			System.err.println("成功发送到服务器！");
		     byte[] buffer = new byte[1024];
		     int len = inputStream.read(buffer);
		     String str = new String(buffer,0,len);
		     System.out.println("排序后数据： "+str);
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			NetTools.closeAll(scanner, socket, inputStream, outputStream);
		}

	}

	public static void main(String[] args) {
       String host = "localhost";
       int  port = 8800;
       SortClient client = new SortClient(host, port);
       client.sort();
       
	}

}
